<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { luckDrawApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import Pagination from "@/components/Pagination/index.vue";
  import table2excel from 'js-table2excel';
  import addUser from './addUser.vue'
  const addUserRef = ref();
  const action = reactive({
    total:0,
    list:[] as any[],
  })
  const listQuery = reactive({
    page:1,
    limit:20,
    extract_pool_id:'',
  })
  const handleDel = (row:any = {})=>{
    const { nickname='', id=0 } = row
    ElMessageBox.confirm(`确定删除抽取礼物池用户：${nickname}吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      const loading = Toastloading();
      await luckDrawApi.wishWheelDelExtractPoolUser({
        id,
      });
      loading.close();
      ToastSuccess('删除成功');
      getList();
    })
    .catch(() => {});
  }
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await luckDrawApi.wishWheelExtractPoolUserList(listQuery)
    loading.close()
    const { list=[], total=0 } = res.data
    action.total = total
    action.list = list
  }
  const handleDownload = async()=>{
    const loading = Toastloading('数据导出中')
    const res = await luckDrawApi.wishWheelExtractPoolUserList({
      page:1,
      extract_pool_id:listQuery.extract_pool_id,
      limit:action.total
    })
    loading.close()
    const { list=[] } = res.data
    const data = list.map((item:any)=>{
      const { is_delete } = item
      return {
        ...item,
        is_delete_text:['未移除','已移除'][is_delete] || is_delete
      }
    })
    const tHeader = [
      { title: '用户UUID', key: "uuid"},
      { title: '昵称', key: "nickname"},
      { title: '公会ID', key: 'union_id'},
      { title: '公会名称', key: 'union_name'},
      { title: '礼物池ID', key: 'extract_pool_id'},
      { title: '礼物池名称', key: 'pool_name'},
      { title: '进入礼物池操作人', key: 'admin_name'},
      { title: '进入时间', key: 'add_time'},
      { title: '状态', key: 'is_delete_text'},
      { title: '移除礼物池操作人', key: 'deladmin_name'},
      { title: '移除时间', key: 'del_time'},
    ]
    table2excel(
      tHeader,
      data,
      `许愿转盘抽取礼物池用户`)
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">抽取礼物池ID：</div>
          <el-input v-model="listQuery.extract_pool_id" placeholder="请输入抽取礼物池ID"></el-input>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" v-rule="'turntable_extractPoolUserListAdd'" @click="addUserRef.openDialog()">新增用户</el-button>
          <el-button type="warning" @click="handleDownload">导出列表</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="120" prop="user_id" label="用户ID" />
      <el-table-column width="120" prop="uuid" label="用户UUID" />
      <el-table-column min-width="150" show-overflow-tooltip prop="nickname" label="昵称" />
      <el-table-column width="120" prop="union_id" label="公会ID" />
      <el-table-column min-width="150" show-overflow-tooltip prop="union_name" label="公会名称" />
      <el-table-column width="120" prop="extract_pool_id" label="礼物池ID" />
      <el-table-column min-width="150" show-overflow-tooltip prop="pool_name" label="礼物池名称" />
      <el-table-column width="180" prop="admin_name" label="进入礼物池操作人" />
      <el-table-column width="180" prop="add_time" label="进入时间" />
      <el-table-column width="180" prop="is_delete" label="状态">
        <template #default="scoped">
          <el-tag
            :type="['','warning','success'][scoped.row.is_delete] || ''"
          >
            {{ ['未移除','已移除'][scoped.row.is_delete] || scoped.row.is_delete }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column width="180" prop="deladmin_name" label="移除礼物池操作人" />
      <el-table-column width="180" prop="del_time" label="移除时间" />
      <el-table-column width="120" fixed="right" label="操作">
        <template #default="scoped">
          <el-button
          v-if="scoped.row.is_delete == 0" type="danger"
          v-rule="'turntable_extractPoolUserListDel'"
          @click="handleDel(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <addUser @update-list="getList" ref="addUserRef" />
  </div>
</template>
